package com.xuyang.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.xuyang.flags.Menus;

import android.util.Log;
//把从服务器获得的字符串解析到menu里
public class AnalysisData {
	public AnalysisData(String str) {
		try {
			JSONObject object = new JSONObject(str);
			JSONArray array = object.names();
			Log.e("ana", array.toString());
			boolean have=false;
			// 完成菜单大项的录入
			for (int i = 0; i <array.length(); i++) {
				if (array.getString(i).equals("酒水饮料")) {
					have=true;
				}else {
					Menus.menuTitles.add(array.getString(i));
				}
			}
			if (have) {
				Menus.menuTitles.add("酒水饮料");
			}
			for (String tableName : Menus.menuTitles) {
				ArrayList<FoodMenu> arrayList = new ArrayList<FoodMenu>();
				JSONArray a = object.getJSONArray(tableName);
				for (int i = 0; i < a.length(); i++) {
					JSONObject o = a.getJSONObject(i);
					FoodMenu food = new FoodMenu(o.getString("foodName"),
							o.getInt("foodPrice"), o.getString("foodUnitPrice"));
					arrayList.add(food);
				}
				Menus.menus.put(tableName, arrayList);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
}
